#include <iostream>

#include "mem_util.h"

using namespace std;


unsigned char code1[] = {
    0x55,                           /* push ebp */
    0x89, 0xE5,                     /* mov ebp, esp */
    0x83, 0xEC, 0x08,               /* sub esp, 8 */
    0x68, 0x00, 0x00, 0x00, 0x00,   /* push 地址，这里需要修改 */
    0x6A, 0x00,                     /* push 0 */
    0xE8, 0x00, 0x00, 0x00, 0x00,   /* call printf 函数地址，这里需要修改 */
    0x83, 0xC4, 0x08,               /* add esp, 8 */
    0x89, 0xEC,                     /* mov esp, ebp */
    0x5D,                           /* pop ebp */
    0xC3                            /* ret */
};

unsigned char code[] = {
    0x83, 0xC0, 0x01, 0xEB, 0xFB
};


int main()
{
    cout << "Hello World!" << endl;

    cout << "input process ID: " << endl;
    DWORD processId;
    cin >> processId;


    xy::OtherProcess	p( processId );
    xy::OtherProcessMemory	pm( &p );
    LPVOID			lp = pm.alloc( 100 );
    cout << "write byts:" << pm.write( lp, code1, sizeof(code1) ) << endl;
    unsigned char d[sizeof(code)];
    cout << "read bytes:" << pm.read( lp, d, sizeof(d) ) << endl;

    xy::RemoteThread rt( &p, (LPTHREAD_START_ROUTINE) lp, NULL );
    cout << rt.start() << endl;
    rt.wait( 60000 );

    xy::File	f( TEXT( "test7.txt" ) );
    xy::FileMapMemory	m( false, nullptr, 0, 1024, &f );
    auto		k = m.data<int*>();
    k[0]	= 100;
    k[1]	= 222;

    for ( int i = 0; i < 100; i++ )
    {
        cout << k[i];
    }
    cout << endl;


    auto da = m.data<char*>();
    for ( int i = 0; i < 600; i++ )
    {
        da[i] = 'S';
    }
    cout << "over" << endl;


    getchar();

    return(0);
}


